package com.powernode.customer.controller;

import com.powernode.common.annotation.PowerLogin;
import com.powernode.common.constant.RedisConstant;
import com.powernode.common.result.Result;
import com.powernode.common.util.AuthContextHolder;
import com.powernode.customer.service.CustomerService;
import com.powernode.model.form.customer.UpdateWxPhoneForm;
import com.powernode.model.vo.customer.CustomerLoginVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "客户API接口管理")
@RestController
@RequestMapping("/customer")
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerController {

    @Resource
    private CustomerService customerService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Operation(summary = "小程序登录")
    @GetMapping("/login/{code}")
    public Result<String> login(@PathVariable String code) {
        return Result.ok(customerService.login(code));
    }



    @PowerLogin
    @Operation(summary = "获取客户登录信息")
    @GetMapping("/getCustomerLoginInfo")
    public Result<CustomerLoginVo> getCustomerLoginInfo(@RequestHeader("token") String token) {

        //String customerId = stringRedisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);

        //从threadLocal中获取用户id
        Long userId = AuthContextHolder.getUserId();
        //删除threadLocal中存储的userId 防止内存泄漏
        AuthContextHolder.removeUserId();

        return Result.ok(customerService.getCustomerLoginInfo(userId));
    }


    @Operation(summary = "更新客户微信手机号码")
    @PostMapping("/updateWxPhone")
    public Result<Boolean> updateWxPhoneNumber(@RequestBody UpdateWxPhoneForm updateWxPhoneForm) {
        //Boolean b = customerService.updateWxPhoneNumber(updateWxPhoneForm);
        //个人开发者获取不到手机号，这里只是让流程跑通
        return Result.ok(true);
    }
}

